package com.fortune.service.impl;

import java.util.List;

import com.fortune.constants.RoleConstants;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.vo.dUserMgt.Positon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fortune.common.paging.PagingData;
import com.fortune.dao.MUserMgtDao;
import com.fortune.dto.in.mUserMgt.MUserAddDto;
import com.fortune.dto.in.mUserMgt.MUserEditDto;
import com.fortune.dto.in.mUserMgt.MUserListDto;
import com.fortune.dto.in.mUserMgt.MUserLogableDto;
import com.fortune.entity.MUserInfo;
import com.fortune.service.MUserMgtService;
import com.fortune.vo.mUserMgt.User;
@Service
public class MUserMgtServiceImpl implements MUserMgtService{
	@Autowired
	private MUserMgtDao mUserMgtDao;
	@Override
	public int addMUser(MUserInfo mUserInfo, MUserAddDto addDto) {
		int checkPhone = mUserMgtDao.checkPhone(mUserInfo, addDto); 
		if(checkPhone>0)
			return 2;
		int checkNumber = mUserMgtDao.checkNumber(mUserInfo, addDto);
		if(checkNumber>0)
			return 3;
		int add = mUserMgtDao.addMUser(mUserInfo, addDto);
		return add;
	}
	@Override
	public PagingData<User> pagingData(MUserInfo mUserInfo, MUserListDto listDto) {
		List<User> list = mUserMgtDao.list(mUserInfo, listDto);
		int count = mUserMgtDao.count(mUserInfo, listDto);
		PagingData<User> pagingData = new PagingData<>(list,count);
		return pagingData;
	}
	@Override
	public int edit(MUserInfo mUserInfo, MUserEditDto editDto) {
		Positon positon = mUserMgtDao.findPositionIsAdmin(mUserInfo.getmId(),editDto.getRoleId());
		if(positon == null){
			return 5;
		}
		int checkUser = mUserMgtDao.checkUser(mUserInfo, editDto);
		if (checkUser==0) {
			return 2;
		}
		int checkEditPhone = mUserMgtDao.checkEditPhone(mUserInfo, editDto);
		if (checkEditPhone>0) {
			return 3;
		}
		int checkNumber = mUserMgtDao.checkNumberEdit(mUserInfo.getmId(),editDto.getUserId(), editDto);
		if(checkNumber>0){
			return 4;
		}
		if(positon.getId().equals("0000")){
			int oo = mUserMgtDao.editEmployers(mUserInfo.getmId(),mUserInfo.getId(),editDto.getName(),editDto.getPhone());
			if(oo > 0){
				int oa =	mUserMgtDao.editAccount(mUserInfo.getmId(),editDto);
				if(oa > 0){
					return oo;
				}else{
					throw new UpdateInnerErrorException("编辑失败");
				}
			}else{
				throw new UpdateInnerErrorException("编辑失败");
			}
		}
		int edit = mUserMgtDao.edit(mUserInfo, editDto);
		return edit;
	}
	@Override
	public int delete(MUserInfo mUserInfo, String userId) {
		String positionId = RoleConstants.SUPER_ADMIN_ID;
		int delete = mUserMgtDao.delete(mUserInfo, userId, positionId);
		return delete;
	}
	@Override
	public int resetPassword(MUserInfo mUserInfo, String userId) {
		int reset = mUserMgtDao.resetPassword(mUserInfo, userId);
		return reset;
	}
	@Override
	public int setLogin(MUserInfo mUserInfo, MUserLogableDto logableDto) {
		int set = mUserMgtDao.set(mUserInfo, logableDto);
		return set;
	}

	@Override
	public List<Positon> findListPosition(String mid) {
		return mUserMgtDao.findListPosition(mid);
	}

}
